%\paragraph{Widocznośc i dziedziczenie.}
W przykładach pominięto include, define i using.
Przykład.

\includegraphics{rys/umlet-cpp-samples-inheritance}

Plik {\sf Person.h}.
\begin{lstCpp}
class Person {
protected:
  string name; 
public:
  void setName(string n) { name = n; }
  string getName(void)   { return name;}
};
\end{lstCpp}

Plik {\sf Student.h}.
\begin{lstCpp}
class Student : public Person {
private:
  int id;
  static int totalStudents;
public:
  Student();
  ~Student();
  int getId();
  void setId(int i);
  static int getTotalStudents();
};
\end{lstCpp}

Plik {\sf Student.cpp}.
\begin{lstCpp}
int Student::totalStudents = 0;

Student::Student() { ++totalStudents; }
Student::~Student() { --totalStudents; 
  cout << "Usuwam " << getId() << ", " << getName() << endl;
  cout << "number of students: " << Student::getTotalStudents() << endl;
}
int Student::getId() { return id; }
void Student::setId(int i) { id = i; }
int Student::getTotalStudents() { return totalStudents; }
\end{lstCpp}

Plik {\sf main.cpp}.
\begin{lstCpp}
int main(void) {
  cout << "number of students: " << Student::getTotalStudents() << endl;
  Student s1;
  s1.setId(10124);
  s1.setName("John Smith");
  cout << "number of students: " << Student::getTotalStudents() << endl;
  cout << "Student s1: " << s1.getId() << ", " << s1.getName() << endl;
  return 0;
}
\end{lstCpp}
Po kompilacji i uruchomieniu
\begin{lstV}
number of students: 0
number of students: 1
Student s1: 10124, John Smith
Usuwam 10124, John Smith
number of students: 0
\end{lstV}